package com.it.controller;

import com.github.pagehelper.PageInfo;
import com.it.mapper.UserInfoMapper;
import com.it.pojo.*;
import com.it.pojo.dto.ProductDto;
import com.it.pojo.dto.TimeDto;
import com.it.service.*;
import com.it.util.ExcelUtils;
import com.it.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: zgc
 * @version: 1.0
 * @description:
 * @date: 2023/12/25 10:49
 */
@Api(value = "ProductController", tags = {"产废管理"})
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private WasteService wasteService;
    @Autowired
    private ContainerService containerService;
    @Autowired
    private ProductService productService;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserInfoService userInfoService;

    /*跳转到产废页面*/
    @ApiOperation("查询所有容器信息(跳转产废页面)")
    @GetMapping("/list")
    public ModelAndView list() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/product/product");
        List<Container> list = containerService.findAll();
        modelAndView.addObject("containerList", list);
        return modelAndView;
    }

    @ApiOperation("查询单个类型下的所有废物")
    @GetMapping("/list/{typeId}")
    @ResponseBody
    public List<Waste> add(@PathVariable("typeId") String typeId) {
        return wasteService.findTypeId(typeId);
    }

    @ApiOperation("查询单个废物信息")
    @GetMapping("/query/{id}")
    public ModelAndView query(@PathVariable("id") String id, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/product/list");
        Waste waste = wasteService.findById(id);
        session.setAttribute("waste",waste);
        return modelAndView;
    }

    @ApiOperation("添加产废记录")
    @PostMapping("/add")
    @ResponseBody
    public R<String> addProduct(Product product, HttpSession session) {
        //获取当前用户
        User user = (User)session.getAttribute("user");
        product.setUserId(user.getUserId());
        product.setUserLoginname(user.getUserLoginname());
        //获取公司id
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(user.getUserId());
        product.setCompanyId(userInfo.getCompanyId());
        //添加方法
        String msg = productService.insertProduct(product);
        return R.error(msg);
    }

    @ApiOperation("根据容器id查询容器")
    @GetMapping("/findById/{id}")
    @ResponseBody
    public R<Container> findById(@PathVariable("id") String conId){
        Container container = containerService.getOneContainer(conId);
        return R.success(container);
    }

    @ApiOperation("查看产废记录")
    @GetMapping("/find")
    public ModelAndView find(ProductDto dto){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/product/list");
        PageInfo<Product> pageInfo = productService.findPage(dto);
        modelAndView.addObject("pageInfo",pageInfo);
        List<UserInfo> users = userInfoService.findAll();
        modelAndView.addObject("users",users);
        List<TimeDto> dtoList = new ArrayList<>();
        dtoList.add(new TimeDto(1,"当天"));
        dtoList.add(new TimeDto(2,"本月"));
        dtoList.add(new TimeDto(3,"本季"));
        dtoList.add(new TimeDto(4,"本年"));
        modelAndView.addObject("timeList",dtoList);
        return modelAndView;
    }

    @PostMapping("/find")
    public ModelAndView one(ProductDto dto){
        System.out.println(dto);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/product/list");
        PageInfo<Product> pageInfo = productService.findPage(dto);
        modelAndView.addObject("pageInfo",pageInfo);
        List<UserInfo> users = userInfoService.findAll();
        modelAndView.addObject("users",users);
        List<TimeDto> dtoList = new ArrayList<>();
        dtoList.add(new TimeDto(1,"当天"));
        dtoList.add(new TimeDto(2,"本月"));
        dtoList.add(new TimeDto(3,"本季"));
        dtoList.add(new TimeDto(4,"本年"));
        modelAndView.addObject("timeList",dtoList);
        return modelAndView;
    }

    @PostMapping("/excel")
    public ModelAndView excel(ProductDto dto, HttpServletResponse response){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/product/find");
        /*导入excel操作*/
        List<Product> excel = productService.findExcel(dto);
        ExcelUtils.export(response,"产废记录",excel,Product.class);
        return modelAndView;
    }



}
